package org.rzp.ideaplug.statistic.project;

import com.intellij.openapi.components.ProjectComponent;
import com.intellij.openapi.module.Module;
import com.intellij.openapi.module.ModuleManager;
import com.intellij.openapi.project.Project;
import com.intellij.openapi.project.ProjectManager;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.rzp.ideaplug.statistic.push.PushStatisticUtil;
import org.rzp.ideaplug.statistic.setting.StatisticPushSettingState;
import org.rzp.ideaplug.statistic.statistic.StatisticInfo;
import org.rzp.ideaplug.statistic.statistic.StatisticUtil;

import java.io.File;
import java.util.*;

@Slf4j
public class ProjectOpenComponent implements ProjectComponent {


    private static Map<String, List<String>> openProjectMap = new HashMap<>();


    @Override
    public void projectOpened() {
        StatisticPushSettingState settingState = StatisticPushSettingState.getInstance();

        ProjectManager projectManager = ProjectManager.getInstance();

        Project[] openProjects = projectManager.getOpenProjects();

        for (Project project : openProjects) {
            List<String> pushFileList = openProjectMap.get(project.getName());
            if (null != pushFileList) {
                continue;
            }
            pushFileList = new ArrayList<>();
            String projectRootPath = new File(project.getBasePath()).getPath();
            ModuleManager moduleManager = ModuleManager.getInstance(project);
            Module[] modules = moduleManager.getModules();
            for (Module m : modules) {

                File moduleRootFile = new File(m.getModuleFilePath()).getParentFile();
                String modulePath = moduleRootFile.getPath();
                if (!moduleRootFile.getName().startsWith(".")) {
                    if (modules.length == 1) {
                        pushFileList.add(modulePath);
                    } else {
                        if (!projectRootPath.equals(modulePath)) {
                            pushFileList.add(modulePath);
                        }
                    }
                }
            }
            openProjectMap.put(project.getName(), pushFileList);

            if (StringUtils.isNotBlank(settingState.url) && StringUtils.isNotBlank(settingState.user)) {
                if (settingState.open) {
                    projectAutoStatisticPush(pushFileList, project, true);
                }
            }

        }
    }

    @Override
    public void projectClosed() {
        StatisticPushSettingState settingState = StatisticPushSettingState.getInstance();

        ProjectManager projectManager = ProjectManager.getInstance();
        Project[] openProjects = projectManager.getOpenProjects();

        Set<String> openKeySet = new HashSet<>();
        for (Project p : openProjects) {
            openKeySet.add(p.getName());
        }

        Set<String> closeKeySet = new HashSet<>();
        Set<String> AllKeySet = openProjectMap.keySet();
        for (String k : AllKeySet) {
            if (!openKeySet.contains(k)) {
                closeKeySet.add(k);
            }
        }

        for (String k : closeKeySet) {
            if (StringUtils.isNotBlank(settingState.url) && StringUtils.isNotBlank(settingState.user)) {
                if (settingState.close) {
                    projectAutoStatisticPush(openProjectMap.get(k), null, false);
                }
            }
            openProjectMap.remove(k);
        }

    }


    private static void projectAutoStatisticPush(List<String> pushFileList, Project project, boolean notify) {
        StatisticPushSettingState settingState = StatisticPushSettingState.getInstance();
        for (String path : pushFileList) {
            StatisticInfo statistic = StatisticUtil.statistic(path);
            if (notify)
                PushStatisticUtil.pushInfoNotify(settingState.user, settingState.url, new File(path).getName(), statistic, project);
            else {
                PushStatisticUtil.pushInfoOnly(settingState.user, settingState.url, new File(path).getName(), statistic);
            }
        }
    }
}
